package com.quwan.manager.controller;


import com.quwan.common.BaseController;
import com.quwan.common.exception.ExpCode;
import com.quwan.common.result.ApiResult;
import com.quwan.entity.Agreement;
import com.quwan.entity.TAdmin;
import com.quwan.manager.controller.dto.AgreementDto;
import com.quwan.manager.service.IAgreementService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 协议表 前端控制器
 * </p>
 *
 * @author quwan
 * @since 2022-01-14
 */
@Api(tags = "后台用户协议管理接口")
@RestController
@RequiredArgsConstructor
@RequestMapping("mgr/agreement")
public class AgreementController extends BaseController {

    private final IAgreementService agreementService;

    @ApiOperation(value = "用户协议列表",notes = "<span style=\"color:red\">注意参数是否必填</span>",httpMethod = "POST")
    @PostMapping("agreementList")
    public ApiResult<List<AgreementDto>> agreementList() {
        List<AgreementDto> res = agreementService.selectAgreementDtoList();
        return ApiResult.success(res);
    }
    @ApiOperation("用户协议修改")
    @PostMapping("updateAgreement")
    public ApiResult updateAgreement(HttpServletRequest request,
                                 @ApiParam(value = "主键id")@RequestParam(required = true) Integer id,
                                 @ApiParam(value = "名称") @RequestParam(required = false)String name,
                                 @ApiParam(value = "状态 0-生效,1-无效") @RequestParam(required = false)Integer status,
                                 @ApiParam(value = "协议类型 1-隐私政策,2-用户协议") @RequestParam(required = false)Integer type,
                                 @ApiParam(value = "协议富文本类容") @RequestParam(required = false)String content,
                                 @ApiParam(value = "修改人id") @RequestParam(required = false)Long lastModifyUserId){
        //TokenInfo tokenInfo = getTokenInfo(request);
        Agreement agreement = new Agreement();
        agreement.setId(Long.valueOf(id));
        agreement.setName(name);
        agreement.setStatus(status);
        agreement.setType(type);
        agreement.setContent(content);
        //agreement.setLastModifyUserId(Long.valueOf(tokenInfo.getId()));
        agreement.setLastModifyUserId(lastModifyUserId);
        agreement.setLastModifyTime(LocalDateTime.now());
        boolean success = agreementService.updateById(agreement);
        if (success)
            return ApiResult.successMsg();
        else
            return ApiResult.failed();
    }

    @PostMapping("addAgreement")
    @ApiOperation("添加用户协议")
    public ApiResult addAgreement(HttpServletRequest request,
                                  @ApiParam(value = "名称") @RequestParam(required = false)String name,
                                  @ApiParam(value = "状态 0-生效,1-无效") @RequestParam(required = false)Integer status,
                                  @ApiParam(value = "协议类型 1-隐私政策,2-用户协议") @RequestParam(required = false)Integer type,
                                  @ApiParam(value = "协议富文本类容") @RequestParam(required = false)String content,
                                  @ApiParam(value = "创建人id") @RequestParam(required = false)Long createUserId){
        TokenInfo tokenInfo = getTokenInfo(request);
        Agreement agreement = new Agreement();
        agreement.setName(name);
        agreement.setStatus(status);
        agreement.setType(type);
        agreement.setContent(content);
        //agreement.setCreateUserId(Long.valueOf(tokenInfo.getId()));
        agreement.setCreateUserId(createUserId);
        agreement.setCreateTime(LocalDateTime.now());
        boolean success = agreementService.save(agreement);
        if (success)
            return ApiResult.successMsg();
        else
            return ApiResult.failed();
    }

    @ApiOperation(value = "用户协议详情",notes = "<span style=\"color:red\">注意参数是否必填</span>",httpMethod = "POST")
    @PostMapping("detailAgreement")
    public ApiResult detailAgreement(@ApiParam(value = "主键id")@RequestParam(required = true) Integer id){
        Agreement agreement = agreementService.getById(id);
        return ApiResult.success(agreement);

    }

    @PostMapping("delAgreement")
    @ApiOperation("删除用户协议")
    public ApiResult delAgreement(@ApiParam(value = "主键id")@RequestParam(value = "id") Integer id){
        boolean success = agreementService.removeById(id);
        if (success)
            return ApiResult.successMsg();
        else
            return ApiResult.failed();
    }
}
